package com.uam.utils.geo.buffer;


import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.operation.buffer.BufferOp;

/**
 * 缓冲区域的基础类
 *
 * @author: heguanglong
 * @date: 2020-12-26
 **/
public class BufferBase {
    public static GeometryFactory geometryFactory = new GeometryFactory();

    public double baseNum=100000;
    public double rate=0.25;
    public double radius(double radius) {
        return radius / (2 * Math.PI * 6371004) * 360;
    }

    /**
     * 经纬度转换 jts识别的点
     *
     * @param str
     * @return
     */
    public Coordinate[] getCoordinate(String str) {
        String[] arr = str.split(";");
        Coordinate[] coordinates4 = new Coordinate[arr.length];
        for (int i = 0; i < arr.length; i++) {
            String[] a = arr[i].split("\\,");
            Coordinate c = new Coordinate(Double.valueOf(a[0]), Double.valueOf(a[1]));
            coordinates4[i] = c;
        }
        return getCoordinate(str,";","\\,");
    }


    public Coordinate[] getCoordinate(String str, String splitAll, String splitGps){
        String[] arr = str.split(splitAll);
        Coordinate[] coordinates4 = new Coordinate[arr.length];
        for (int i = 0; i < arr.length; i++) {
            String[] a = arr[i].split(splitGps);
            Coordinate c = new Coordinate(Double.valueOf(a[0]), Double.valueOf(a[1]));
            coordinates4[i] = c;
        }
        return coordinates4;
    }

    /**
     * 获取缓冲区域
     *
     * @param gmt  实体图
     * @param type
     * @param rd   缓冲去距离
     * @return
     */
    public Geometry getBuff(Geometry gmt, int type, double rd) {
        BufferOp bufOp = new BufferOp(gmt);
        bufOp.setEndCapStyle(type);
        bufOp.setQuadrantSegments(18);
        return bufOp.getResultGeometry(radius(rd));
    }

    /**
     * 获取缓冲区域转换成数组
     */
    public String gpsToString(Coordinate[] gps) {
        if (gps != null) {
            StringBuilder stringBuilderBuffer = new StringBuilder();
            for (Coordinate currentCoordinates : gps) {
                double lat = currentCoordinates.x;
                double lon = currentCoordinates.y;
                stringBuilderBuffer.append(lat).append(",").append(lon).append(";");
            }
            return stringBuilderBuffer.deleteCharAt(stringBuilderBuffer.length() - 1).toString();
        }
        return null;
    }
}
